package com.xky.beetlsql.controller;

import com.xky.beetlsql.entity.Account;
import com.xky.beetlsql.service.AccountService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author keying.xia
 * @date 2017/12/11
 */
@RestController
@RequestMapping("/beetlsql")
public class Controller {

    private Logger logger = LoggerFactory.getLogger(Controller.class);

    @Autowired
    private AccountService accountService;

    @ApiOperation(value = "查询用户",notes = "根据用户的id进行查询")
    @ApiImplicitParam(name = "id",value = "用户的id",dataType = "Integer",paramType = "path",required = true)
    @GetMapping(value = "/get/{id}")
    public Account getAccount(@PathVariable Integer id){
        logger.info("id : " + id);
        return accountService.getAcccountByID(id);
    }

    @ApiOperation(value = "查询所有用户",notes = "查询所有的用户")
    @GetMapping(value = "/getAll")
    public List<Account> getAccounts(){
        return accountService.getAccountList();
    }

    @ApiOperation(value = "新增用户",notes = "新增用户")
    @PostMapping(value = "/add")
    public void addAccount(@RequestBody Account account){
        accountService.addAccount(account);
    }

    @ApiOperation(value = "删除某个用户",notes = "删除某个用户")
    @ApiImplicitParam(name = "id",value = "用户的id",dataType = "Integer",paramType = "path",required = true)
    @DeleteMapping(value = "/delete/{id}")
    public void deleteAccount(@PathVariable Integer id){
        accountService.deleteAccount(id);
    }

    @ApiOperation(value = "更新用户",notes = "更新某个用户")
    @ApiImplicitParam(name = "id",value = "用户的id",dataType = "Integer",paramType = "path",required = true)
    @PutMapping(value = "/update/{id}")
    public int updateAccount(@PathVariable Integer id,@RequestBody Account account){
        return accountService.updateAccount(id,account);
    }

    @ApiOperation(value = "查找用户",notes = "查找某个用户")
    @ApiImplicitParam(name = "name",value = "用户的名称",dataType = "String",paramType = "path",required = true)
    @GetMapping(value = "/getAccountByName/{name}")
    public Account getAccount(@PathVariable String name){
        return accountService.getAccountByName(name);
    }


    @ApiOperation(value = "查找用户",notes = "根据年纪来查找用户")
    @ApiImplicitParams(
            {
                @ApiImplicitParam(name = "minAge",value = "下限年纪",required = true,dataType = "Integer",paramType = "path"),
                @ApiImplicitParam(name = "maxAge",value = "上限年纪",required = true,dataType = "Integer",paramType = "path")
            }
    )
    @GetMapping(value = "/getAccountByAge/{minAge}/{maxAge}")
    public List<Account> getAccountsByAge(@PathVariable Integer minAge, @PathVariable Integer maxAge){
        return   accountService.getAccountsByAge(minAge,maxAge);
    }

    @ApiOperation(value = "查询用户",notes = "根据name查询用户")
    @ApiImplicitParam(name = "name",value = "用户的name",dataType = "String",required = true,paramType = "path")
    @GetMapping(value = "/getAccountsLikeName/{name}")
    public List<Account> getAccountsLikeName(@PathVariable String name){
        return accountService.getAccountsLikeName(name);
    }

    @ApiOperation(value = "多表查询用户",notes = "多表查询用户")
    @GetMapping(value = "/getAccountsMore")
    public List<Account> getAccountsMore(){
        List<Account> accounts = accountService.getAccounts();
        for (Account account: accounts){
            logger.info("sex :" + account.get("sex"));
        }
        return accounts;
    }


}
